
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;



public class ListStack<E> implements Stack<E> {

    Nodo<E> head;
    int elements;

    public ListStack() {
        head = null;
        elements = 0;
    }

    public boolean isEmpty() {
        return head == null;
    }

    public E peek() {
        if (head == null) {
            return null;
        } else {
            return head.key;
        }
    }

    public E pop() throws EmptyStackException {
        if (isEmpty()) {
            throw new EmptyStackException();
        } else {
            elements--;
            Nodo<E> nn = head;
            head = nn.next;
            return nn.key;
        }
    }

    public void push(E push) throws FullStackException{
        if (isFull())
            throw new FullStackException();
        else{
            elements++;
            head = new Nodo<E>(push, head);
        }
    }

    public boolean isFull() {
        return elements==10;
    }

    @Override
    public String toString() {
        ArrayList temp = new ArrayList();
        ArrayList temp2 = new ArrayList();
        int cont = 0;
        
        try {
            for (int i = 4; i > -2; i--) {
                String t = (String) this.pop();
                temp.add(t);
                System.out.println("Ocupado: " + t);
            }
        } catch (Exception e) {
            System.out.println("Los demas estan vacios");
        }
        for(int k=0 ; k<temp.size() ; k++){
            try {
                this.push((E) temp.get(k));
            } catch (FullStackException ex) {
                
            }
        }
        return "";
    }
}
class Nodo<T> {

    public T key;
    public Nodo<T> next;

    public Nodo(T key, Nodo<T> next) {
        this.key = key;
        this.next = next;
    }
}